
#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
    @author jay.han
"""

import logging

from cvirt.cnode.libvirtXmlObject.baseDomain import XmlObject
LOG = logging.getLogger(__name__)

class GuestClock(XmlObject):
    ROOT_NAME = 'clock'
    def __init__(self, parse_xml=None, **kw):
        super(GuestClock, self).__init__(parse_xml=parse_xml,**kw)

        self.offset = "localtime"
        self.adjustment = None
        self.timezone = None
        self.timers = []

    def _parse_xml(self,xmldoc):

        self.offset = xmldoc.get('offset')
        for c in xmldoc.getchildren():
            if c.tag == 'adjustment':
                self.adjustment = c.get('adjustment')
            elif c.tag == 'timezone':
                self.timezone = c.get('timezone')


    def _get_xml_config(self, root):
        clk = root

        clk.set("offset", self.offset)
        if self.adjustment:
            clk.set("adjustment", self.adjustment)
        elif self.timezone:
            clk.set("timezone", self.timezone)

        for tm in self.timers:
            clk.append(tm.get_xml_config())

        return clk

    def add_timer(self, tm):
        self.timers.append(tm)
